/**
 * 
 */
package com.tz.online.cart.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.tz.online.cart.service.IShoppingCart;
import com.tz.online.entity.OrderItem;


/**
 * 使用内存来存储实现的购物车
 * @author Administrator
 *
 */
public class MemoryCart implements IShoppingCart {

	//属性
	private Map<Long, OrderItem> items = new HashMap<>();
	private double total; //总费用
	private int count; //商品总数量
	
	/* (non-Javadoc)
	 * @see com.tz.exercise.cart.service.IShoppingCart#addCart(com.tz.exercise.entity.OrderItem)
	 */
	@Override
	public void addCart(OrderItem item) {
		if(item != null) {
			boolean flag = false;
			//迭代整个集合，判断当前的map中，是否包含了这个 item对象
			Set<Long> keys = items.keySet();
			for(Long bookId : keys) {
				//比较
				if(bookId.equals(item.getBookId())) {
					//说明，此map中已经有了这个 OrderItem中的图书
					flag = true;
					break;
				} 
			}
			//判断
			if(flag) {
				//说明存在
				OrderItem oi = items.get(item.getBookId());
				oi.setCount(oi.getCount()+1);
			} else {
				//说明不存在
				items.put(item.getBookId(), item);
			}
			
			//最后，要更新 total和count 属性
			update();
		}
		
	}

	/****
	 * 此方法的目的就是更新total属性和count属性值
	 */
	private void update() {
		//先清0
		this.total = 0;
		this.count = 0;
		//迭代
		Set<Long> keys = items.keySet();
		//
		for(Long bookId : keys) {
			OrderItem value = items.get(bookId);
			//把费用添加起来
			this.total += (value.getPrice()*value.getCount());
			//把商品总数加起来
			this.count += value.getCount();
		}
	}

	/* (non-Javadoc)
	 * @see com.tz.exercise.cart.service.IShoppingCart#clear()
	 */
	@Override
	public void clear() {
		this.items.clear();
		this.total = 0.0;
		this.count = 0;
	}

	/* (non-Javadoc)
	 * @see com.tz.exercise.cart.service.IShoppingCart#modifyCount(java.lang.Long, int)
	 */
	@Override
	public void modifyCount(Long bookId, int count) {
		//
		OrderItem oi = find(bookId);
		//修改数量
		if(oi != null) {
			//进一步验证参数的有效性
			if(count > 0) {
				oi.setCount(count);
				//更新属性
				update();
			} else {
				throw new RuntimeException("修改的数量不合法");
			}
		}
	}

	/* (non-Javadoc)
	 * @see com.tz.exercise.cart.service.IShoppingCart#delete(java.lang.Long)
	 */
	@Override
	public void delete(Long bookId) {
		//
		OrderItem oi = find(bookId);
		//
		if(oi != null) {
			items.remove(bookId);
			//更新
			update();
		}
		
	}

	/*private OrderItem find(Long bookId) {
		OrderItem oi = null;
		//
		Set<Long> keys = items.keySet();
		for(Long bid : keys) {
			if(bid.equals(bookId)) {
				oi = items.get(bookId);
				break;
			}
		}
		//返回
		return oi;
	}
*/
	/* (non-Javadoc)
	 * @see com.tz.exercise.cart.service.IShoppingCart#getTotal()
	 */
	@Override
	public double getTotal() {
		return this.total;
	}

	/* (non-Javadoc)
	 * @see com.tz.exercise.cart.service.IShoppingCart#getCount()
	 */
	@Override
	public int getCount() {
		return this.count;
	}
	
	@Override
	public Map<Long, OrderItem> getItems() {
		return this.items;
	}

	@Override
	public OrderItem find(Long bookId) {
		OrderItem oi = null;
		//
		Set<Long> keys = items.keySet();
		for(Long bid : keys) {
			if(bid.equals(bookId)) {
				oi = items.get(bookId);
				break;
			}
		}
		//返回
		return oi;
	}

}
